package org.example.test;

import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import org.apache.rocketmq.common.message.Message;
import org.springframework.test.annotation.Rollback;

/**
 * @Auther:Liu
 * @Testname:App
 * @Date:2025/8/25 11:44
 */
@SpringBootTest
@Slf4j
@Component
public class App {
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 同步消息
     */
    @Test
    public void sync() {
        String message = "Hello RocketMQ";
        SendResult sendResult = rocketMQTemplate.syncSend("TopicB:sync", message);
        log.info(sendResult.toString());
    }

    /**
     * 异步消息
     */
    @SneakyThrows
    @Test
    public void async() {
        String message = "Hello RocketMQ";
        rocketMQTemplate.asyncSend("TopicB:async", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info(sendResult.toString());
            }

            @Override
            public void onException(Throwable throwable) {
                log.error(throwable.getMessage());
            }
        });
        Thread.sleep(10000);
    }

    /**
     * 单向消息
     */
    @Test
    public void OneWay() {
        String message = "Hello RocketMQ";
        rocketMQTemplate.sendOneWay("TopicB:oneWay", message);
    }


    /**
     * 过滤
     */
    @SneakyThrows
    @Test
    public void OneWay2() {
        Message message = new Message("TopicB", "guolv", "Hello RocketMQ".getBytes());
        message.putUserProperty("age",18+"");
        DefaultMQProducer producer = rocketMQTemplate.getProducer();
        producer.send(message);
    }
}
